package com.pcbly.web.bean.news;

import java.util.HashSet;
import java.util.Set;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;



/**
 * @authtor Lingo.
 * @since 2007-08-22
 */
@SuppressWarnings("unchecked")
public class NewsCategoryBean extends AbstractTreeEntityBean {
    /** * serial. */
    static final long serialVersionUID = 0L;

    /** * 位编码策略. */
    public static final int STRATEGY_BIT_CODE = 0;

    /** * 字符编码策略. */
    public static final int STRATEGY_CHAR_CODE = 1;

    /** * 递归策略. */
    public static final int STRATEGY_RECURSION = 2;

    /** * id. */
    private String id;

    /** * parent. */
    private NewsCategoryBean parent;

    /** * name. */
    private String name;

    /** * theSort. */
    private Integer sort;

    /** * status. */
    private Integer status;

    /** * children. */
	private Set<NewsCategoryBean> children = new HashSet<NewsCategoryBean>(0);

    /** * newses. */
    private Set<NewsBean> newses = new HashSet<NewsBean>(0);

    /** * bitCode. */
    private Long bitCode;

    /** * charCode. */
    private String charCode;

    /** * 构造方法. */
    public NewsCategoryBean() {
    }

    /** * @return id. */
    public String getId() {
        return id;
    }

    /** * @param id id. */
    public void setId(String id) {
        this.id = id;
    }

    /** * @return NewsCategory. */
    public NewsCategoryBean getParent() {
        return parent;
    }

    /** * @param parent NewsCategory. */
    public void setParent(NewsCategoryBean parent) {
        this.parent = parent;
    }

    /** * @return name. */
    public String getName() {
        return name;
    }

    /** * @param name name. */
    public void setName(String name) {
        this.name = name;
    }

   

    /** * @return status. */
    public Integer getStatus() {
        return status;
    }

    /** * @param status status. */
    public void setStatus(Integer status) {
        this.status = status;
    }

 

    /** * @return newses. */
    public Set<NewsBean> getNewses() {
        return newses;
    }

    /** * @param newses newses. */
    public void setNewses(Set<NewsBean> newses) {
        this.newses = newses;
    }

    // ====================================================
    // 编码
    // ====================================================

    /** * @return bitCode. */
    public Long getBitCode() {
        return bitCode;
    }

    /** * @param bitCode Long. */
    public void setBitCode(Long bitCode) {
        this.bitCode = bitCode;
    }

    /** * @return charCode. */
    public String getCharCode() {
        return charCode;
    }

    /** * @param charCode String. */
    public void setCharCode(String charCode) {
        this.charCode = charCode;
    }

    /**
     * 分类深度.
     *
     * @return int
     */
    public int getLevel() {
        return getLevelByBitCode();
    }

    /**
     * 根据位编码计算深度.
     *
     * @return int
     */
    public int getLevelByBitCode() {
        if (bitCode == null) {
            if (parent == null) {
                return 1;
            } else {
                return parent.getLevel() + 1;
            }
        } else {
            for (int i = 7; i > 0; i--) {
                long base = 1L << (8 * i);

                if ((bitCode % base) == 0) {
                    return 8 - i;
                }
            }

            return 8;
        }
    }

    /**
     * 根据字符编码计算深度.
     *
     * @return int
     */
    public int getLevelByCharCode() {
        if (charCode == null) {
            if (parent == null) {
                return 1;
            } else {
                return parent.getLevel() + 1;
            }
        } else {
            return charCode.length() / 2;
        }
    }

    /**
     * 使用递归计算深度.
     *
     * @return int
     */
    public int getLevelByRecursion() {
        int level = 1;
        NewsCategoryBean upper = parent;

        while (parent != null) {
            upper = upper.getParent();
            level++;
        }

        return level;
    }

	
	@Override
	public void setParent(TreeEntityBean treeentitybean) {
		// TODO Auto-generated method stub
	}

	@Override
	public Set<NewsCategoryBean> getChildren() {
		// TODO Auto-generated method stub
		return children;
	}

	@Override
	public void setChildren(Set children) {
		// TODO Auto-generated method stub
		this.children=children;
	}

	public void setSort(Integer sort) {
		this.sort = sort;
	}

	public Integer getSort() {
		return sort;
	}
	/**
	 * @see java.lang.Object#equals(Object)
	 */
	public boolean equals(Object object) {
		return EqualsBuilder.reflectionEquals(this, object);
	}

	/**
	 * @see java.lang.Object#hashCode()
	 */
	public int hashCode() {
		return HashCodeBuilder.reflectionHashCode(this);
	}

	/**
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		return ToStringBuilder.reflectionToString(this,
				ToStringStyle.MULTI_LINE_STYLE);
	}
}
